﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class admin_ManageArea : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AdminCs.CheckLoginState();
        if (!IsPostBack)
        {

            Bind();

        }
    }
    private void Bind()
    {
        string strSQL, strOneid = "", strTwoid = "";
        if (Request.QueryString["oneid"] != null && Request.QueryString["twoid"] != null)
        {
            strOneid = Request.QueryString["oneid"].ToString();
            strTwoid = Request.QueryString["twoid"].ToString();
        }
        if (strOneid == "" && strTwoid == "")
        {
            Lbl_Navigation.Text = "所有地区";
            Lbl_Title.Text = "一级地区管理";
            strSQL = "select * from Area where oneid<>0 and twoid=0 and threeid=0";


        }
        else
        {
            if (strOneid != "0" && strTwoid == "0")
            {
                strSQL = "select areaname from Area where twoid=0 and threeid=0 and oneid=" + strOneid;

                Lbl_Navigation.Text = "<a href='managearea.aspx'>所有地区</a>->" + Class1.GetSqlResult(strSQL);
                Lbl_Title.Text = "二级地区管理";
                strSQL = "select * from Area where twoid<>0 and threeid=0 and oneid=" + strOneid;

            }
            else
            {
                strSQL = "select areaname from Area where twoid=0 and threeid=0 and oneid=" + strOneid;

                Lbl_Navigation.Text = "<a href='managearea.aspx'>所有地区</a>-><a href='managearea.aspx?oneid=";
                Lbl_Navigation.Text += strOneid + "&twoid=0'>" + Class1.GetSqlResult(strSQL) + "</a>->";
                strSQL = "select areaname from Area where threeid=0 and oneid=" + strOneid + " and twoid=" + strTwoid;
                Lbl_Navigation.Text += Class1.GetSqlResult(strSQL);
                Lbl_Title.Text = "三级地区管理";
                strSQL = "select * from Area where threeid<>0 and oneid=" + strOneid + " and twoid=" + strTwoid;

            }

        }

        strSQL += " order by sort";
        Gvw_List.DataSource = Class1.GetDataReader(strSQL);
        Gvw_List.DataKeyNames = new string[] { "id", "oneid", "twoid", "threeid" };
        Gvw_List.DataBind();
        if (Gvw_List.Rows.Count > 0)
        {
            ((Button)Gvw_List.Rows[0].Cells[2].Controls[0]).Visible = false;
            ((Button)Gvw_List.Rows[Gvw_List.Rows.Count - 1].Cells[3].Controls[0]).Visible = false;

        }

    }
    protected void Gvw_List_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            ((Button)e.Row.Cells[2].Controls[0]).CommandArgument = e.Row.RowIndex.ToString();
            ((Button)e.Row.Cells[3].Controls[0]).CommandArgument = e.Row.RowIndex.ToString();

        }
    }
    protected void Gvw_List_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int i = 0;
        if (e.CommandName == "up")
        {
            i = -1;
        }
        if (e.CommandName == "down")
        {
            i = 1;
        }
        if (i != 0)
        {
            string sort1 = Gvw_List.Rows[Convert.ToInt16(e.CommandArgument)].Cells[0].Text;
            string sort2 = Gvw_List.Rows[Convert.ToInt16(e.CommandArgument) + i].Cells[0].Text;
            string id1 = Gvw_List.DataKeys[Convert.ToInt16(e.CommandArgument)]["id"].ToString();
            string id2 = Gvw_List.DataKeys[Convert.ToInt16(e.CommandArgument) + i]["id"].ToString();

            string strSQL = "update Area set sort=" + sort2 + " where id=" + id1;
            Class1.ExecCommand(strSQL);
            strSQL = "update Area set sort=" + sort1 + " where id=" + id2;
            Class1.ExecCommand(strSQL);


            Bind();
        }
    }
    protected void Gvw_List_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Gvw_List.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void Gvw_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = Gvw_List.DataKeys[e.RowIndex]["id"].ToString();
        string areaname = ((TextBox)Gvw_List.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string strSQL = "update Area set areaname='" + areaname + "' where id=" + id;
        Class1.ExecCommand(strSQL);
        Gvw_List.EditIndex = -1;
        Bind();
    }
    protected void Gvw_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string oneid = Gvw_List.DataKeys[e.RowIndex]["oneid"].ToString();
        string twoid = Gvw_List.DataKeys[e.RowIndex]["twoid"].ToString();
        string threeid = Gvw_List.DataKeys[e.RowIndex]["threeid"].ToString();
        string strSQL = "", strSQL1 = "";
        if (oneid != "0" && twoid == "0" && threeid == "0")
        {
            strSQL = "delete from Area where oneid=" + oneid;
            //strSQL1 = "delete from nts_infodata where area_oneid=" + oneid;


        }
        if (oneid != "0" && twoid != "0" && threeid == "0")
        {
            strSQL = "delete from Area where oneid=" + oneid + " and twoid=" + twoid;
            //strSQL1 = "delete from nts_infodata where area_oneid=" + oneid + " and area_twoid=" + twoid;


        }
        if (oneid != "0" && twoid != "0" && threeid != "0")
        {
            strSQL = "delete from Area where oneid=" + oneid + " and twoid=" + twoid + " and threeid=" + threeid;
            //strSQL1 = "delete from nts_infodata where area_oneid=" + oneid + " and area_twoid=" + twoid + " and area_threeid=" + threeid;

        }

        Class1.ExecCommand(strSQL);
        //strSQL = strSQL1;
        //Class1.ExecCommand(strSQL);
        Bind();
    }
    protected void Gvw_List_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        if (Gvw_List.DataKeys[e.NewSelectedIndex]["threeid"].ToString() != "0")
        {
            Response.Write("<script>alert('已经是末级分类，无法进入！')</script>");
            return;

        }
        string oneid = Gvw_List.DataKeys[e.NewSelectedIndex]["oneid"].ToString();
        string twoid = Gvw_List.DataKeys[e.NewSelectedIndex]["twoid"].ToString();
        Response.Redirect("managearea.aspx?oneid=" + oneid + "&twoid=" + twoid);
    }
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        string strSQL, strOneid = "", strTwoid = "";
        if (Request.QueryString["oneid"] != null && Request.QueryString["twoid"] != null)
        {
            strOneid = Request.QueryString["oneid"].ToString();
            strTwoid = Request.QueryString["twoid"].ToString();
        }


        //添加一级分类
        if (strOneid == "" && strTwoid == "")
        {
            strSQL = "select max(oneid)+1,max(sort)+1 from Area ";
            strSQL += "where oneid<>0 and twoid=0 and threeid=0";
            IDataReader dr = Class1.GetDataReader(strSQL);
            dr.Read();
            string oneid = "1", sort = "1";
            if (dr[0].ToString() != "" && dr[1].ToString() != "")
            {
                oneid = dr[0].ToString();
                sort = dr[1].ToString();
            }
            strSQL = "insert into Area (areaname,oneid,twoid,threeid,sort) values ('";
            strSQL += Txt_Name.Text + "'," + oneid + ",0,0," + sort + ")";

            dr.Close();

        }
        else
        {
            if (strOneid != "0" && strTwoid == "0")  //添加二级分类
            {

                strSQL = "select max(twoid)+1,max(sort)+1 from Area ";
                strSQL += "where twoid<>0 and threeid=0 and oneid=" + strOneid;
                IDataReader dr = Class1.GetDataReader(strSQL);
                dr.Read();
                string twoid = "1", sort = "1";
                if (dr[0].ToString() != "" && dr[1].ToString() != "")
                {
                    twoid = dr[0].ToString();
                    sort = dr[1].ToString();
                }
                strSQL = "insert into Area (areaname,oneid,twoid,threeid,sort) values ('";
                strSQL += Txt_Name.Text + "'," + strOneid + "," + twoid + ",0," + sort + ")";

                dr.Close();

            }
            else
            {
                //添加三级分类
                strSQL = "select max(threeid)+1,max(sort)+1 from Area ";
                strSQL += "where threeid<>0 and oneid=" + strOneid + " and twoid=" + strTwoid;
                IDataReader dr = Class1.GetDataReader(strSQL);
                dr.Read();
                string threeid = "1", sort = "1";
                if (dr[0].ToString() != "" && dr[1].ToString() != "")
                {
                    threeid = dr[0].ToString();
                    sort = dr[1].ToString();
                }

                strSQL = "insert into Area (areaname,oneid,twoid,threeid,sort) values ('";
                strSQL += Txt_Name.Text + "'," + strOneid + "," + strTwoid + ",";
                strSQL += threeid + "," + sort + ")";

                dr.Close();

            }

        }
        Class1.ExecCommand(strSQL);
        Bind();
    }


    protected void Btn_Js_Click(object sender, EventArgs e)
    {
        //将地区信息写入area.js文件
        //添加二级地区
        string strAreaData, strSQL;
        strAreaData = "<script language = 'JavaScript' type='text/javascript'>var onecount;onecount=0;subcat = new Array();";
        strSQL = "select * from Area where oneid<>0 and twoid<>0 and threeid=0 order by sort";
        IDataReader dr = Class1.GetDataReader(strSQL);
        int i1 = 0;
        while (dr.Read())
        {
            strAreaData += "subcat[" + i1.ToString() + "] = new Array('" + dr["areaname"].ToString() + "','" + dr["oneid"].ToString() + "','" + dr["twoid"].ToString() + "');";
            i1++;

        }
        strAreaData += "onecount=" + i1.ToString() + ";";

        //添加三级地区
        strAreaData += "var onecount1;onecount1=0;subcat1 = new Array();";

        strSQL = "select * from Area where oneid<>0 and twoid<>0 and threeid<>0 order by sort";
        dr = Class1.GetDataReader(strSQL);
        int n1 = 0;
        while (dr.Read())
        {
            strAreaData += "subcat1[" + n1.ToString() + "] = new Array('" + dr["areaname"].ToString() + "','" + dr["oneid"].ToString() + "','" + dr["twoid"].ToString() + "','" + dr["threeid"].ToString() + "');"; n1++;

        }
        dr.Close();
        strAreaData += "onecount1=" + n1.ToString() + ";</script>";

        //StreamWriter str;
        //File.Delete(Server.MapPath("~/js/area.js"));

        //str = File.CreateText(Server.MapPath("~/js/area.js"));
        //str.WriteLine(strAreaData);
        //str.Close();

        //Response.Write("<script>alert('成功生成JS文件！')</script>");
        return;
    }
    protected void Gvw_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        Gvw_List.EditIndex = -1;
        Bind();
    }
}
